package com.ten.course.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ten.common.core.entity.EduChapter;
import com.ten.common.core.entity.EduVideo;
import com.ten.common.core.vo.Result;
import com.ten.course.dao.CourseChapterDao;
import com.ten.course.dao.CourseVideoDao;
import com.ten.course.service.CourseChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author ：张成（3092211427@qq.com）
 * @date ：Created in 2022/12/21 23:14
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class CourseChapterServiceImpl implements CourseChapterService {
    @Autowired
    private CourseChapterDao courseChapterDao;

    @Autowired
    private CourseVideoDao courseVideoDao;

    /**
     * 根据课程id查询课程的章节
     *
     * @param courseId 课程id
     * @return Result<List < EduChapter>>
     */
    @Override
    public Result<List<EduChapter>> findChapterById(String courseId) {
        //根据课程id查询出对应的章节
        List<EduChapter> eduChapters = courseChapterDao.selectList(new QueryWrapper<EduChapter>().eq("course_id", courseId));
        for (EduChapter eduChapter : eduChapters) {
            //根据对应的章节id查询视频信息
            List<EduVideo> eduVideos = courseVideoDao.selectList(new QueryWrapper<EduVideo>().eq("chapter_id", eduChapter.getId()));
            eduChapter.setVideo(eduVideos);
        }
        return new Result<>(200, "查询成功", eduChapters);
    }

    /**
     * 根据章节id对章节进行更新或者添加
     *
     * @param eduChapter
     * @return
     */
    @Override
    public Result insertChapterWithCourseId(EduChapter eduChapter) {
        // EduChapter(id=1408410059079483394, courseId=1408409971229319170, title=第一部分DDD概览：为什么需要DDD, sort=1, gmtCreate=2021-06-25T21:01:40, gmtModified=2021-06-25T21:01:40)
        //如果传入的数据中含有id，则是修改
        if (StringUtils.hasText(eduChapter.getId())) {
            UpdateWrapper<EduChapter> wrapper = new UpdateWrapper<>();
            wrapper.eq("id", eduChapter.getId());
            int update = courseChapterDao.update(eduChapter, wrapper);
            if (update > 0) {
                return new Result(200, "修改成功");
            }
        } else {
            //不含有id，则是添加
            QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
            wrapper.eq("title", eduChapter.getTitle());
            EduChapter chapter = courseChapterDao.selectOne(wrapper);
            if (chapter != null) {
                return new Result(500, "你要添加的章节已经存在，不要重复添加");
            }
            long snowflakeNextId = IdUtil.getSnowflakeNextId();
            eduChapter.setId(snowflakeNextId + "");
            int insert = courseChapterDao.insert(eduChapter);
            if (insert > 0) {
                return new Result(200, "保存成功", eduChapter);
            }
        }
        return null;
    }

    /**
     * 删除章节
     *
     * @param eduChapter
     * @return
     */
    @Override
    public Result deleteChapterWithCourseId(EduChapter eduChapter) {
        int i = courseChapterDao.deleteById(eduChapter.getId());
        if (i > 0) {
            return new Result(200, "删除成功");
        }
        return new Result(500, "删除失败");
    }
}